<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Response;
use think\Session;
use think\Cookie;


class aboutbanner extends Login
{
    // public function _initialize()
    // {
    //     $request = Request::instance();
    //     // $this->error("抱歉！没有操作权限！");

    //     (var_dump($request->module(),$request->controller(),$request->action()));

    // }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1)
    {
        //查询gqx_admin,role,user_role表
        $count = Db::name('about_banner')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('about_banner')->order(['id'=>'asc'])->page($page,10)->select();
        return view('about/indexBanner',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::name('role')->select();
        // return dump($data);
        return view('about/addBanner',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();

        $file = $request->file('photo');
        $info = $file->validate(['size'=>1567800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static/aboutus/img');
        if($info){
        // 成功上传后 获取上传信息
        // 输出 42a79759f284b767dfcb2a0197904287.jpg
            $data['photo'] =  $info->getSaveName();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
        if($data['display'] == 'on'){
            $data['display'] = 1;
        }
        $res = Db::name('about_banner')->insert($data);
        if($res){
            $this->redirect('admin/aboutbanner/index', ['cate_id' => 2]);
        }
    }

    /**
     * 显示指定的搜索资源
     *
     * @param  int  $like
     * @return \think\Response
     */
    public function search($page = 1,Request $request)
    {
        $post = $request->post();
        $count = Db::name('about_banner')->where('photo','like','%'.$post['like'].'%')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('about_banner')->where('photo','like','%'.$post['like'].'%')->order(['id'=>'asc'])->page($page,10)->select();
        return view('about/indexBanner',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // return $id;
        $arr = Db::name('admin')->where('id',$id)->find();
        $data = Db::name('role')->select();
        $rid = Db::name('user_role')->where('user_id',$id)->find()['role_id'];
        // var_dump($arr);
        // return ;
        return view('admin/edit',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'arr'=>$arr,
            'data'=>$data,
            'rid'=>$rid
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        // return $id;
        // return $request->param();
        // 直接处理状态更改请求
        if(@$request->param()['source'] == 'display'){
            $data['display'] = $request->param()['status'];
            return Db::name('about_banner')->where('id',$id)->update($data);
        }
        //正常处理编辑流程
        $arr = $request->param();

        $info['display']= (@$arr['display']=='1' ? 1 : 0) ;
        $u = Db::name('about_banner')->where('id',$id)->update($info);
        if ($u){
            return $u;
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        return Db::name('about_banner')->where('id',$id)->delete();
    }
}
